import axios from "axios";

import router from "../router"

import ElementUI from "element-ui";

axios.defaults.baseURL = process.env.NODE_ENV === 'development' ? "/api" : "http://www.chst.vip"

axios.defaults.withCredentials = true; 

import NProgress from "nprogress"
export let CancelToken = axios.CancelToken;
axios.interceptors.request.use(config => {
    if (config.url == "/users/login") {
        return config
    } else {
        let token = localStorage.getItem('wf-token')
        config.headers['authorization'] = token;
        return config
    }

})
axios.interceptors.response.use(config => {

    let { data } = config;
    if (data.code == '1004' || data.code == '10022') {
        ElementUI.Message.error("登入信息失效,请重新登入")
        localStorage.removeItem("wf-token")
        router.push("/login")
        window.location.reload()
    }
    return config
})

axios.create({
    timeout: 4000
})


export default axios